define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

        var Controller = {
            index: function () {
                // 初始化表格参数配置
                Table.api.init({
                    extend: {
                        index_url: 'check/service/index',
                        add_url: 'check/service/add',
                        edit_url: 'check/service/edit',
                        del_url: 'check/service/del',
                        multi_url: 'check/service/multi',
                        table: 'service',
                    }
                });

                var table = $("#table");
                table.on('post-body.bs.table', function (e, settings, json, xhr) {
                    $(".btn-audit").data("area", ["90%", "90%"]);
                    $(".btn-detail").data("area", ["90%", "90%"]);
                    $(".btn-detail").data("area", ["90%", "90%"]);
                    $(".btn-assign").data("area", ["50%", "50%"]);
                    $(".btn-update_time").data("area", ["50%", "50%"]);
                    $(".btn-d5").data("area", ["80%", "80%"]);
                });
                var check_id = [];
                // 初始化表格
                table.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    pk: 'id',
                    sortName: 'id',
                    searchFormVisible: true,
                    maintainSelected: 1,
                    showSearch: false,
                    search: false,
                    onCheck: function (row, tr, flied) {
                        check_id.push(row.id)
                        //  console.log(row);
                    },
                    columns: [
                        [
                            {
                                checkbox: true, formatter: function (value, row, index) {
                                    //                                        if (row.check == 1) {
                                    //                                            return { checked: true };
                                    //                                        }

                                    if (check_id.includes(row.id)) {
                                        return {checked: true};
                                    }
                                }
                            },
                            {field: 'lifts.lift_number', title: __('电梯编号'), operate: 'like'},
                            {field: 'specifications', title: __('电梯规格'), operate: false},
                            {
                                field: 'specifications_name',  title: '电梯规格', searchList: function (column) {
                                    return Template('specifications_name', {});
                                }, formatter: function (value, row, index) {
                                    return '无';
                                }, visible: false
                            },
                            {field: 'lifts.real_name', title: '业主姓名', operate: 'like'},
                            {field: 'lifts.phone', title: '联系电话', operate: 'like'},
                            {field: 'lifts.area', title: '所在地区', operate: false},
                            {
                                field: 'address1',  title: '所在地区', searchList: function (column) {
                                    return Template('address1', {});
                                }, formatter: function (value, row, index) {
                                    return '无';
                                }, visible: false
                            },
                            {field: 'lifts.specific_address', title: '电梯地址', operate: 'like'},
                            {field: 'service_time', title: '服务时间',formatter: Table.api.formatter.datetime,
                                operate: 'RANGE',
                                addclass: 'datetimerange',
                                sortable: true},
                            {field: 'cleaner_name', title: '保洁员',operate: false},
                            {
                                field: 'cleaning_id', title: '保洁员',
                                operate: '=',
                                addclass: 'selectpage',
                                extend: 'data-source="check/service/getSaleMan" data-field="title"',
                                editable: false,
                                visible: false,
                                // editable: {
                                //     type: 'select',
                                //     pk: 1,
                                //     source: Config.MyclassList
                                // },
                            },
                            {
                                field: 'status', title: __('服务状态'),
                                searchList: {
                                    //                                "1": __('待助力'),
                                    "1": __('待服务'),
                                    "2": __('服务中'),
                                    "3": __('已评价'),
                                    "4": __('未评价'),
                                    "5": __('售后'),
                                },
                                formatter: function (value, row) {
                                    if (value == 1) {
                                        return '<span style="">待服务</span>';
                                    } else if (value == 2) {
                                        return '<span style="">服务中</span>';
                                    } else if (value == 3) {
                                        return '已评价';
                                    } else if (value == 4) {
                                        return '未评价';
                                    } else if (value == 5) {
                                        return '售后';
                                    }
                                }
                            },
                            {
                                field: 'freeze', title: __('订单状态'),
                                searchList: {
                                    //                                "1": __('待助力'),
                                    "1": __('冻结'),
                                    "2": __('未冻结'),
                                },
                                formatter: function (value, row) {
                                    if (value == 1) {
                                        return '<span style="">冻结</span>';
                                    } else if (value == 2) {
                                        return '<span style="">未冻结</span>';
                                    }
                                }
                            },
                            {field: 'package', title: '套餐名称', operate: false},
                            {
                                field: 'package_name',  title: '套餐名称', searchList: function (column) {
                                    return Template('package_name', {});
                                }, formatter: function (value, row, index) {
                                    return '无';
                                }, visible: false
                            },
                            {field: 'duration', title: '套餐时长', operate: false},
                            {
                                field: 'duration_name',  title: '套餐时长', searchList: function (column) {
                                    return Template('duration_name', {});
                                }, formatter: function (value, row, index) {
                                    return '无';
                                }, visible: false
                            },

//                        { field: 'elevator_no', title: '电梯编号', operate: 'like' },
//                        {
//                            field: 'clean_area', title: __('清洁范围'),
//                            searchList: {
//                                //                                "1": __('待助力'),
//                                "1": __('轿厢内部四个面'),
//                                "2": __('轿厢内部四个面+地板+厅门'),
//                            },
//                            formatter: function (value) {
//                                switch (value) {
//                                    case 1:
//                                        value = '轿厢内部四个面';
//                                        break;
//                                    case 2:
//                                        value = '轿厢内部四个面+地板+厅门';
//                                        break;
//
//                                }
//                                return value;
//                            }
//                        },
                            {
                                field: 'add_time',
                                title: __('创建时间'),
                                formatter: Table.api.formatter.datetime,
                                operate: 'RANGE',
                                addclass: 'datetimerange',
                                sortable: true
                            },
                            {
                                field: 'cleaning_end_time',
                                title: __('清洁结束时间'),
                                formatter: Table.api.formatter.datetime,
                                operate: 'RANGE',
                                addclass: 'datetimerange',
                                sortable: true
                            },

                            {
                                field: 'operate', title: __('Operate'), table: table, buttons: [
                                    {
                                        name: 'detail',
                                        text: __('查看详情'),
                                        title: __('查看详情'),
                                        classname: 'btn btn-xs btn-success btn-detail btn-magic btn-dialog',
                                        icon: 'fa fa-eye',
                                        url: 'check/service/detail?id={id}',
                                        // extend:'data-area=["60%","70%"]',
                                        callback: function (data) {
                                        },
                                    },
                                    {
                                        name: 'update_time',
                                        text: __('修改服务时间'),
                                        title: __('修改服务时间'),
                                        classname: 'btn btn-xs btn-warning btn-update_time btn-magic btn-dialog',
                                        icon: 'fa fa-paint-brush',
                                        url: 'check/service/edit?id={id}',
                                        // extend:'data-area=["60%","70%"]',
                                        callback: function (data) {
                                        },
                                    },
                                    {
                                        name: 'assign',
                                        text: __('指派保洁员'),
                                        title: __('指派保洁员'),
                                        classname: 'btn btn-xs btn-danger btn-assign btn-magic btn-dialog',
                                        icon: 'fa fa-paint-brush',
                                        url: 'check/service/do_assign?id={id}',
                                        // extend:'data-area=["60%","70%"]',
                                        callback: function (data) {
                                        },
                                    },
                                    // {
                                    //     name: 'see_service_order',
                                    //     text: __('查看服务单'),
                                    //     title: __('查看服务单'),
                                    //     classname: 'btn btn-xs btn-success btn-magic btn-addtabs',
                                    //     icon: 'fa fa-eye',
                                    //     url: 'check/order/see_service_order?id={id}',
                                    //     // extend:'data-area=["60%","70%"]',
                                    //     callback: function (data) {
                                    //     },
                                    // },
                                    {
                                        name: 'after_sale',
                                        text: __('申请售后'),
                                        title: __('申请售后'),
                                        classname: 'btn btn-xs btn-danger btn-assign btn-magic btn-dialog',
                                        icon: 'fa fa-paint-brush',
                                        url: 'check/service/service_after_sale?id={id}',
                                        // extend:'data-area=["60%","70%"]',
                                        callback: function (data) {
                                        },
                                    },
                                    // {
                                    //     name: 'freeze',
                                    //     text: __('冻结'),
                                    //     title: __('冻结'),
                                    //     classname: 'btn btn-xs btn-danger btn-assign btn-magic btn-dialog',
                                    //     icon: 'fa fa-paint-brush',
                                    //     url: 'check/service/freeze',
                                    //     // extend:'data-area=["60%","70%"]',
                                    //     callback: function (data) {
                                    //     },
                                    // },
                                    {
                                        name: 'up_status',
                                        text: __('冻结'),
                                        title: __('冻结'),
                                        classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                        url: 'check/service/freeze?status=1',
                                        confirm: '确定要冻结该服务单吗',
                                        success: function (data, ret) {
                                          //  Layer.alert('冻结成功');
                                            $(".btn-refresh").trigger("click");
                                            //如果需要阻止成功提示，则必须使用return false;
                                            //return false;
                                        },
                                        error: function (data, ret) {
                                            console.log(data, ret);
                                        //    Layer.alert(ret.msg);
                                            return false;
                                        },

                                    },
                                    {
                                        name: 'down_status',
                                        text: __('解冻'),
                                        title: __('解冻'),
                                        classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                        url: 'check/service/freeze?status=2',
                                        confirm: '确定要解冻该服务单吗',
                                        success: function (data, ret) {
                                           // Layer.alert('解冻成功');
                                            $(".btn-refresh").trigger("click");
                                            //如果需要阻止成功提示，则必须使用return false;
                                            //return false;
                                        },
                                        error: function (data, ret) {
                                            console.log(data, ret);
                                          //  Layer.alert(ret.msg);
                                            return false;
                                        },

                                    },
                                ], events: Table.api.events.operate, formatter: function (value, row, index) {
                                    var that = $.extend({}, this);
                                    if(row.free_clean_id >0){
                                        if(row.freeze==1){
                                            $(table).data("operate-down_status", true);
                                            $(table).data("operate-up_status", false);
                                        }else{
                                            $(table).data("operate-down_status", false);
                                            $(table).data("operate-up_status", true);
                                        }
                                    }else{
                                        $(table).data("operate-down_status", false);
                                        $(table).data("operate-up_status", false);
                                    }

                                    $(table).data("operate-edit", null); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                    $(table).data("operate-del", null); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                    that.table = table;
                                    return Table.api.formatter.operate.call(that, value, row, index);
                                }
                            },
                        ]
                    ]
                });
                // 为表格绑定事件
                Table.api.bindevent(table);
                $(document).on("click", ".btn-most-assign", function () {
                    var id = $(this).attr('val');
                    console.log(check_id.join(","));
                    //var nick_name=$(this).attr('nickname');
                    Fast.api.open('check/free_clean/do_most_assign?check_id=' + check_id.join(",") + '', __('批量派送'), {area: ['50%', '50%']}, {
                        callback: function (value) {

                        }
                    });
                });
            },
            add: function () {
                Controller.api.bindevent();
            },
            edit: function () {
                Controller.api.bindevent();
            },
            do_most_assign: function () {
                Controller.api.bindevent();
            },
            //解冻
            freeze: function () {
                Controller.api.bindevent();
            },
            //申请售后
            service_after_sale: function () {
                Controller.api.bindevent();
            },
            //审核
            audit: function () {
                Controller.api.bindevent();
                let audit_status1 = document.getElementById('row[audit_status]-1')
                let audit_status2 = document.getElementById('row[audit_status]-0')

                //监听是开票的情况
                audit_status1.addEventListener('click', function () {
                    $('.bh').hide();
                });

                //监听不是开票的情况
                audit_status2.addEventListener('click', function () {
                    $('.bh').show();
                });
            },
            //指派业务员
            do_assign: function () {
                Controller.api.bindevent();
                var is_have_saleman = Config.is_have_saleman;
                if (is_have_saleman == 1) {
                    $('.cleaner_name').show();
                } else {
                    $('.cleaner_name').hide();
                }
                var service_id = Config.service_id;
                Table.api.init({
                    extend: {
                        index_url: 'check/service/cleaner_list?service_id=' + service_id,
                    }
                });
                var table = $("#table");
                // 初始化表格
                table.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    pk: 'id',
                    sortName: 'id',
                    onCheck: function (row, $element) {//bootstrapTable行点击事件 //点击某行时执行
                        $('#uid').val(row.uid);
                    },
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', visible: false, title: __('id'),},
                            {field: 'cleaner_name', title: __('姓名'), operate: false},
                            {field: 'phone', title: __('联系电话'), operate: false},
                            {field: 'count', title: __('今日持单量'), operate: false},
                            {field: 'distance', title: __('离服务点距离'), operate: false},
                        ]
                    ]
                });
                // 为表格绑定事件
                Table.api.bindevent(table);
            },


            content: function () {
                // 初始化表格参数配置
                Table.api.init({
                    extend: {
                        index_url: 'train/train/content',
                        add_url: 'train/train/add_content',
                        edit_url: 'train/train/edit_content',
                        del_url: 'train/train/del_content',
                        multi_url: 'train/train/multi',
                        table: 'content',
                    }
                });

                var table = $("#table");

                // 初始化表格
                table.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    pk: 'id',
                    sortName: 'id',
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', title: __('Id'), sortable: true},
                            {field: 'train_title', title: '内培标题',},
                            {field: 'train.title', title: '类型名称',},
                            {
                                field: 'train_status', title: __('状态'),
                                searchList: {
                                    "1": __('开启'),
                                    "0": __('关闭'),
                                },
                                formatter: function (value) {
                                    switch (value) {
                                        case 0:
                                            value = '关闭';
                                            break;
                                        case 1:
                                            value = '开启';
                                            break;

                                    }
                                    return value;
                                }
                            },
                            {
                                field: 'create_time',
                                title: __('添加时间'),
                                formatter: Table.api.formatter.datetime,
                                operate: 'RANGE',
                                addclass: 'datetimerange',
                                sortable: true
                            },
                            {
                                field: 'operate', title: __('Operate'), table: table, buttons: [
                                    // {
                                    //     name: 'city',
                                    //     text: __('配置对应内容'),
                                    //     title: __('配置对应内容'),
                                    //     classname: 'btn btn-xs btn-success btn-magic btn-addtabs',
                                    //     icon: 'fa fa-eye',
                                    //     url: 'train/train/content?title_id={id}',
                                    //     // extend:'data-area=["60%","70%"]',
                                    //     callback: function (data) {
                                    //     },
                                    // },
                                ], events: Table.api.events.operate, formatter: function (value, row, index) {
                                    var that = $.extend({}, this);
                                    $(table).data("operate-edit", true); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                    $(table).data("operate-del", true); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                    that.table = table;
                                    return Table.api.formatter.operate.call(that, value, row, index);
                                }
                            },
                        ]
                    ]
                });

                // 为表格绑定事件
                Table.api.bindevent(table);
            }
            ,
            add_content: function () {
                Controller.api.bindevent();
            }
            ,
            edit_content: function () {
                Controller.api.bindevent();
            },


            api: {
                bindevent: function () {
                    Form.api.bindevent($("form[role=form]"));
                }
            }
        };
        return Controller;
    }
)
;

function select_teacher() {
    var that = this;
    var opt = $("select[name='row[user_type]']").val();
    var multiple = $(this).data("multiple") ? $(this).data("multiple") : false;
    var mimetype = $(this).data("mimetype") ? opt : 0;
    var admin_id = $(this).data("admin-id") ? $(this).data("admin-id") : '';
    var user_id = $(this).data("user-id") ? $(this).data("user-id") : '';
    var teacher_id = $("#teacher_id").val();
    var school_id = $("#school_id").val();

    parent.Fast.api.open("trusteeship/my_class/select_teacher?element_id=" + teacher_id + "&school_id=" + school_id + "&multiple=" + multiple + "&mimetype=" + mimetype + "&admin_id=" + admin_id + "&user_id=" + user_id, __('Choose'), {
        callback: function (data) {
            console.log(data.id)
            console.log(data.name)
            $("#teacher_id").val(data.id)
            $("#teacher").val(data.name)
        }
    });
}

function select_class_room() {
    var that = this;
    var opt = $("select[name='row[user_type]']").val();
    var multiple = $(this).data("multiple") ? $(this).data("multiple") : false;
    var mimetype = $(this).data("mimetype") ? opt : 0;
    var admin_id = $(this).data("admin-id") ? $(this).data("admin-id") : '';
    var user_id = $(this).data("user-id") ? $(this).data("user-id") : '';
    var teacher_id = $("#teacher_id").val();
    var school_id = $("#school_id").val();
    if (!$("#plan_name").val()) {
        $("#store_id").val(0)
        store_id = 0;
    }
    parent.Fast.api.open("trusteeship/my_class/select_class_room?element_id=" + teacher_id + "&school_id=" + school_id + "&multiple=" + multiple + "&mimetype=" + mimetype + "&admin_id=" + admin_id + "&user_id=" + user_id, __('Choose'), {
        callback: function (data) {
            console.log(data.id)
            console.log(data.name)
            $("#classroom_id").val(data.id)
            $("#classroom").val(data.name)
        }
    });
}

function select_student() {
    var that = this;
    var opt = $("select[name='row[user_type]']").val();
    var multiple = $(this).data("multiple") ? $(this).data("multiple") : false;
    var mimetype = $(this).data("mimetype") ? opt : 0;
    var admin_id = $(this).data("admin-id") ? $(this).data("admin-id") : '';
    var user_id = $(this).data("user-id") ? $(this).data("user-id") : '';
    var teacher_id = $("#teacher_id").val();
    var school_id = $("#school_id").val();
    parent.Fast.api.open("reception/sheet/select_student?element_id=" + teacher_id + "&school_id=" + school_id + "&multiple=" + multiple + "&mimetype=" + mimetype + "&admin_id=" + admin_id + "&user_id=" + user_id, __('Choose'), {
        callback: function (data) {
            console.log(data.id);
            console.log(data.name);
            $("#student_id").val(data.id);
            $("#student_name").val(data.student_name);
            $("#student_name1").val(data.student_name);
            $("#student_name2").val(data.student_name);
            $("#c-image").val(data.image);
            $("#phone").val(data.phone);
            $("#parents").val(data.parents);
            $("#school_class_id").val(data.school_class_id);
            $("#school_class").val(data.school_class);
        }
    });
}